package leetcode.剑指offer;


import java.util.*;

public class 包含min函数的栈30 {
    Stack<Integer> stack = new Stack<>();
    List<Integer> list = new ArrayList<>();
    Integer min = Integer.MAX_VALUE;
    public void push(int x) {
        if(x < min)
            min = x;
        stack.push(x);
        list.add(x);
    }

    public void pop() {
        Integer pop = stack.pop();
        if(Objects.equals(pop, min)){
            list.sort((x, y) -> (x < y) ? 1 : ((x.equals(y)) ? 0 : -1));
            list.remove(list.size()-1);
            min = list.size() != 0 ? list.get(list.size()-1):Integer.MAX_VALUE;
        }else {
            list.remove(pop);
        }
    }

    public int top() {
        Integer a = stack.pop();
        stack.push(a);
        return a;
    }

    public int min() {
        return min;
    }

    public static void main(String[] args) {
        包含min函数的栈30 test = new 包含min函数的栈30();
        test.push(5);
        test.push(5);
        test.push(10);
        test.top();
        test.pop();
        System.out.println(test.min);
        test.pop();
        test.pop();
        test.push(-11);
        test.pop();
        System.out.println(test.top());
        System.out.println(test.min);
    }
}
